import 'package:enterprise_v6_app/app/components/common/custom_no_data.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/certificate_file_entity.dart';
import 'package:enterprise_v6_app/app/modules/profile/cert/cert_controller.dart';

class HomeMyCertView extends StatelessWidget {
  HomeMyCertView({super.key});

  final controller = Get.put(HomeMyCertController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '资质证照',
            ),
            body: Container(
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
              child: controller.fileList.isEmpty
                  ? const CustomNoData()
                  : SingleChildScrollView(
                      child: Column(children: controller.fileList.map(_buildView).toList()),
                    ),
            ),
          );
        });
  }

  Widget _buildView(CertificateFileEntity entity) {
    final List<String> imageUrls = [];
    if (entity.certificateBackUrl!.isNotEmpty) {
      imageUrls.add(entity.certificateBackUrl!);
    }
    if (entity.certificateFrontUrl!.isNotEmpty) {
      imageUrls.add(entity.certificateFrontUrl!);
    }
    return Container(
      margin: EdgeInsets.only(bottom: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
      ),
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      child: Row(
        children: [
          SizedBox(
            width: 120.w,
            height: 80.w,
            child: PageView.builder(
              itemCount: imageUrls.length,
              itemBuilder: (context, index) {
                return Container(
                  decoration: BoxDecoration(
                    color: const Color(0xffF7F7FB),
                    border: Border.all(color: const Color(0xffEBEDF0), width: 1.w),
                    borderRadius: BorderRadius.circular(8.w),
                    image: DecorationImage(
                      image: NetworkImage(Interface.fileUrl + imageUrls[index]),
                      fit: BoxFit.cover,
                    ),
                  ),
                ).clickShowLarge(url: Interface.fileUrl + imageUrls[index]);
              },
            ),
          ),
          AppGaps.hGap12,
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                entity.certificateName!,
                style: TextStyle(
                  fontSize: 16.sp,
                  color: const Color(0xFF262F38),
                  fontWeight: FontWeight.w500,
                ),
              ),
              AppGaps.vGap12,
              Row(
                children: [
                  Text(
                    '证件有效期: ',
                    style: TextStyle(
                      fontSize: 14.sp,
                      height: 1.2,
                      color: const Color(0xFF7C7D93),
                    ),
                  ),
                  Text(
                    entity.becomeDueDate!.isNotEmpty ? entity.becomeDueDate!.substring(0, 10) : '',
                    style: TextStyle(
                      fontSize: 14.sp,
                      height: 1.2,
                      color: const Color(0xFF262F38),
                    ),
                  ),
                ],
              ),
            ],
          )
        ],
      ),
    );
  }
}
